package com.videos.config;

import com.commons.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author mis_wu
 * @date 2021/12/12 19:09
 * videos微服务配置拦截器  TokenInterceptor
 */
@Configuration
public class TokenInterceptorMvcConfig implements WebMvcConfigurer {

    private static final String videosApi = "/videos-services";

    //需要拦截的路径，/**表示需要拦截所有请求
    private String[] addPathPatterns={"/**"};
    //不需要拦截的路径
    private String [] excludePathPaterns={
//            videosApi+"/test",
            videosApi+"/queryCommonsMovies"
    };

//    @Resource
//    private TokenInterceptor tokenInterceptor;

    @Bean
    public TokenInterceptor getRequestToken(){
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //链式编程  registry.addInterceptor(tokenInterceptor).addPathPatterns("/**");//拦截所有请求
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(getRequestToken());
        //拦截所有请求
        interceptorRegistration.addPathPatterns(addPathPatterns);
        //不拦截的请求(登录、注册...)
        interceptorRegistration.excludePathPatterns(excludePathPaterns);
    }
}
